package app.netant.book.yftxt;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class YftxtDetail extends BaseDetail {

	public YftxtDetail(String netSite) {
		super(netSite);
		this.preHref = "http://www.yftxt.com";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select(".downlist .w960.center.clear.mt1 .pleft .viewbox .title h2");
		if(parts.size()>0){
			String title = parts.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".rar");
		}
	}

	@Override
	public void analyseTags() {
		Elements parts1 = this.select(".downlist .w960.center.clear.mt1 .pleft .viewbox .infolist small");
		Elements parts2 = this.select(".downlist .w960.center.clear.mt1 .pleft .viewbox .infolist span");
		for(int i=0;i<parts1.size();i++){
			if(parts2.size()>i){
				String tagName = parts1.get(i).text();
				String tagValue = parts2.get(i).text().trim();
				if(tagName.endsWith("：")){
					tagName = tagName.substring(0,tagName.length()-1).trim();
				}
				System.out.println("【Tag】："+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
		}
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select(".downlist .w960.center.clear.mt1 .pleft .viewbox .content");
		if(parts.size()>1){
			String memo = parts.get(1).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}else if(parts.size()>0){
			String memo = parts.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements parts = this.select(".downlist .w960.center .pleft .viewbox .content ul.downurllist2 li a");
		for(int i=0;i<parts.size();i++){
			
			String text = parts.get(i).text();
			if(text.indexOf("下载")>=0){
				
				String refer = parts.get(i).attr("href");
				System.out.println("【Refer】:"+refer);
				this.entity.setRefer(refer);
				
				YftxtDetail detail = new YftxtDetail(refer);
				String href = detail.stepHref();
				System.out.println("【Href】:"+href);
				this.entity.setDownPath(href);
				break;
				
			}

		}
	}
	
	public String stepHref(){
		Elements parts = this.select(".downlist table tbody tr td .w960 .viewbox table tbody tr td .content ul.downurllist2 strong li a");
		if(parts.size()>0){
			String href = parts.get(0).attr("href");
			return href;
		}
		return null;
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select(".downlist .w960.center.clear.mt1 .pleft .place a");
		if(parts.size()>3){
			String type = parts.get(2).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}else if(parts.size()>2){
			String type = parts.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		YftxtDetail detail = new YftxtDetail("http://www.yftxt.com/txtzg/201808/32315.html");
		detail.test();
	}



}
